﻿using SuperX.Common.Helper;
using Newtonsoft.Json;
using System;
using System.Reflection;

namespace SuperX.Helper
{
    public interface IFuncion
    {
        /// <summary>
        /// 方法名
        /// </summary>
        string MethodName { get; set; }

        object Invoke(Para request);
    }

    public class FuncionInfo : IFuncion
    {
        public string MethodName { get; set; }

        public MethodInfo Method { get; set; }

        public object ExecObj { get; set; }

        public FastInvokeHandler Handler { get; set; }

        public object Invoke(Para request)
        {
            var paras = Method.GetParameters();
            for (int i = 0; i < paras.Length; i++)
            {
                request.Paras[i] = JsonHelper.JsonDeserialize(JsonHelper.JsonSerialize(request.Paras[i]), paras[i].ParameterType);
            }
            return Handler.Invoke(ExecObj, request?.Paras);
        }
    }

    public class Para
    {
        public string MethodName { get; set; }

        public object[] Paras { get; set; }

        [JsonIgnore]
        public DateTime Time { get; set; } = DateTime.Now;
    }
}